// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers.script;

import java.util.ArrayList;
import java.util.List;

import org.dengues.commons.ui.swt.table.AbstractTableViewer;
import org.dengues.commons.utils.DenguesTextUtil;
import org.dengues.commons.utils.FileUtils;
import org.dengues.model.component.ComponentFactory;
import org.dengues.model.component.IMPORTType;
import org.dengues.warehouse.i18n.Messages;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-4-25 qiang.zhang $
 * 
 */
public class JarListTableViewer extends AbstractTableViewer {

    List<IMPORTType> imports;

    /**
     * Qiang.Zhang.Adolf@gmail.com JarListTableViewer constructor comment.
     * 
     * @param parent
     * @param imports
     * @param isReadOnly
     */
    public JarListTableViewer(Composite parent, List<IMPORTType> imports) {
        super(parent, false);
        this.imports = imports;
        setTitle(Messages.getString("JarListTableViewer.title")); //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.commons.ui.swt.table.AbstractTableViewer#initTableInput()
     */
    @Override
    public void initTableInput() {
        super.initTableInput();
        tableViewer.setInput(imports);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.commons.ui.swt.table.AbstractTableViewer#addColumns()
     */
    @Override
    public void addColumns() {
        super.addColumns();
        createColumnName();
        createColumnPath();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createColumnPath".
     */
    private void createColumnPath() {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText(Messages.getString("JarListTableViewer.path")); //$NON-NLS-1$
        column.setWidth(200);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createNameColumn".
     */
    private void createColumnName() {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText(Messages.getString("JarListTableViewer.name")); //$NON-NLS-1$
        column.setWidth(DEFAULT_COLUMN_WITH);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.commons.ui.swt.table.AbstractTableViewer#attachCellEidtor()
     */
    @Override
    public void attachCellEidtor() {
        String[] props = new String[] { SPACES, PROP_NAME, PROP_DEFAULT };
        tableViewer.setColumnProperties(props);
        List<CellEditor> cells = new ArrayList<CellEditor>();
        for (String prop : props) {
            CellEditor editor = new TextCellEditor(table);
            if (prop.equals(PROP_DEFAULT)) {
                editor = new DialogCellEditor(table) {

                    @Override
                    protected Object openDialogBox(Control cellEditorWindow) {
                        FileDialog dialog = new FileDialog(cellEditorWindow.getShell());
                        dialog.setFilterExtensions(new String[] { "*.jar", "*.zip" }); //$NON-NLS-1$ //$NON-NLS-2$
                        String open = dialog.open();
                        if (open != null) {
                            return FileUtils.getPortablePath(open);
                        }
                        return null;
                    }

                };
            }
            cells.add(editor);
        }
        tableViewer.setCellEditors(cells.toArray(new CellEditor[0]));
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.commons.ui.swt.table.AbstractTableViewer#attachProvider()
     */
    @Override
    public void attachProvider() {
        tableViewer.setCellModifier(new ICellModifier() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
             */
            public boolean canModify(Object element, String property) {
                if (property.equals(SPACES)) {
                    return false;
                }
                return true;
            }

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
             */
            public Object getValue(Object element, String property) {
                IMPORTType module = (IMPORTType) element;
                if (PROP_NAME.equals(property)) {
                    return module.getNAME();
                } else if (PROP_DEFAULT.equals(property)) {
                    return module.getMODULE();
                }
                return ""; //$NON-NLS-1$
            }

            public void modify(Object element, String property, Object value) {
                TableItem tableItem = ((TableItem) element);
                IMPORTType module = (IMPORTType) tableItem.getData();
                if (PROP_NAME.equals(property)) {
                    module.setNAME(DenguesTextUtil.getNotNullString(value.toString()));
                } else if (PROP_DEFAULT.equals(property)) {
                    module.setMODULE(DenguesTextUtil.getNotNullString(value.toString()));
                }
                tableViewer.refresh();
            }
        });
        tableViewer.setContentProvider(new IStructuredContentProvider() {

            public Object[] getElements(Object inputElement) {
                if (inputElement instanceof List) {
                    return ((List) inputElement).toArray(new Object[0]);
                }
                return new Object[0];
            }

            public void dispose() {
            }

            public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

            }

        });
        tableViewer.setLabelProvider(new ITableLabelProvider() {

            public Image getColumnImage(Object element, int columnIndex) {
                return null;
            }

            public String getColumnText(Object element, int columnIndex) {
                IMPORTType module = (IMPORTType) element;
                if (columnIndex == 0) {
                    return ""; //$NON-NLS-1$
                } else if (columnIndex == 1) {
                    return module.getNAME();
                } else if (columnIndex == 2) {
                    return module.getMODULE();
                }
                return null;
            }

            public void addListener(ILabelProviderListener listener) {

            }

            public void dispose() {

            }

            public boolean isLabelProperty(Object element, String property) {
                return false;
            }

            public void removeListener(ILabelProviderListener listener) {

            }

        });
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.commons.ui.swt.table.AbstractTableViewer#configAddButtonListener()
     */
    @Override
    public SelectionListener configAddButtonListener() {
        return new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                IMPORTType imType = ComponentFactory.eINSTANCE.createIMPORTType();
                imType.setTYPE("CUSTOM"); //$NON-NLS-1$
                imType.setNAME("jar1"); //$NON-NLS-1$
                imType.setMODULE(""); //$NON-NLS-1$
                imports.add(imType);
                tableViewer.refresh();
            }

        };
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.commons.ui.swt.table.AbstractTableViewer#configRemoveButtonListener()
     */
    @Override
    public SelectionListener configRemoveButtonListener() {
        return new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                IStructuredSelection selection = (IStructuredSelection) tableViewer.getSelection();
                List list = selection.toList();
                for (Object object : list) {
                    imports.remove(object);
                }
                tableViewer.refresh();
            }

        };
    }

}
